*Main do-file for university discrimination
*Setup everything:
clear all
version 15.1
set more off





*Set table directory:
local table_folder "C:\Users\Joakim Jansson\Dropbox\Personliga saker\Skola\Projekt\1 Tentarättning på neken före och efter anonymitet\Björns projekt\Same-sex bias Macro\Tables"
*Data folder:
local nek_folder "C:\Users\Joakim Jansson\Dropbox\Personliga saker\Skola\Projekt\1 Tentarättning på neken före och efter anonymitet\Björns projekt\Same-sex bias Macro\Data\Final_data"
*Temp folder for creating plot:
local temp_folder "C:\Users\Joakim Jansson\Dropbox\Personliga saker\Skola\Projekt\1 Tentarättning på neken före och efter anonymitet\Björns projekt\Same-sex bias Macro\temp_folder"
*Graph folder:
local graph_folder "C:\Users\Joakim Jansson\Dropbox\Personliga saker\Skola\Projekt\1 Tentarättning på neken före och efter anonymitet\Björns projekt\Same-sex bias Macro\Graphs"

*Set cd just to be on safe side:
cd "C:\Users\Joakim Jansson\Dropbox\Personliga saker\Skola\Projekt\1 Tentarättning på neken före och efter anonymitet\Björns projekt\Same-sex bias Macro\Data\Final_data"

*Install necessary packages:
ssc install estout
ssc install ivreg2
ssc install coefplot
ssc install ranktest
ssc install parmest

*Create table containing number of TAs and students per exam:
use "`nek_folder'\quick_nek_long", clear
*No TAs per exam:
egen tag_TA=tag(date teacherid)
egen no_TA=total(tag_TA), by(date)
*No students per exam:
egen tag_student=tag(date newid)
egen no_student=total(tag_student), by(date)
*No female TAs per exam:
egen tag_TA_female=tag(date teacherid) if female_teacher==1
egen no_femaleTA=total(tag_TA_female), by(date)

*Collapse to make one observation of each date:
collapse (mean) no_TA no_student no_femaleTA HT09 points12, by(date)

*Generate American date format:
gen am_date=substr(date,-4,2)+"/"+substr(date,-2,2)+"/"+substr(date,1,4)
*Make normal date of more understandable format:
replace date=substr(date,1,4)+"-"+substr(date,5,2)+"-"+substr(date,7,2)

*Generate table:
estpost tabstat no_student no_TA no_femaleTA HT09 points12, by(date)
matrix A = e(no_student)', e(no_TA)', e(no_femaleTA)', e(HT09)', e(points12)' 
esttab matrix(A) using "`table_folder'\numberTAs.rtf", title("Number of exams and graders by date") nomtitle replace




*Look at diff in diff between genders for multiple choice, and per TA gender:
use "`nek_folder'\quick_nek_long", clear
eststo clear
*Difference-in-difference:
*Age:
eststo: ivreg2 age_exam female09 female_student HT09, cl(teacherid newid)
*Retake:
eststo: ivreg2 retake female09 female_student HT09, cl(teacherid newid)
*Ethnic minority name:
eststo: ivreg2 ethn_min female09 female_student HT09, cl(teacherid newid)
*Swedish name:
eststo: ivreg2 sweden female09 female_student HT09, cl(teacherid newid)
*Multiplc score:
eststo: ivreg2 mltplc_points female09 female_student HT09, cl(teacherid newid)
*Essay score:
eststo: ivreg2 std_score female09 female_student HT09, cl(teacherid newid)

esttab using "`table_folder'\Balancing_diff_diff.rtf", s(N) se label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA and student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace

*Look at probability to continue with economics:
use "`nek_folder'\quick_nek_long", clear
eststo clear
*Collapse in order get weighting of observations right, since students might answer different no of questions:
collapse (mean) future_econ b_level c_level, by(newid date female_student female09 HT09 term pass_course)
tab date b_level
*Seems fine to use everything before term 11!
tab date c_level
*Seems fine up until term 8!
*Use up to term 8 (fall 2011) to be on safe side in both cases
label var b_level "Econ minor"
label var c_level "Econ major"


*Cluster at student level only:
eststo: ivreg2 b_level female09 female_student HT09 if term<9, cl(newid)

eststo: ivreg2 c_level female09 female_student HT09 if term<9, cl(newid)

esttab using "`table_folder'\future_econ_diff_diff.rtf", s(N) se label title(Table X: Probability of future economic studies) addnote("Note: Standard errors clustered at the student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace



*Coefficient plot:
******************
use "`nek_folder'\quick_nek_long", clear

quietly tab  term, gen(term_fe)
*Generate necessary interactions:
forvalues l=1/14 {
	*Female*date:
	gen term_female`l'=female_student*term_fe`l'
}

*Adding question fes:
egen question =  group(date questionno)
quietly tab question, gen(question_fes)

egen program_fem =  group(program_stud female_student)
quietly tab program_fem, gen(program_fes)

ivreg2 std_score term_female1-term_female2 term_female4-term_female14 question_fes1-question_fes194 female_student age_exam sweden ethn_min program_fes1-program_fes73, cl(teacherid newid)
parmest, saving("`temp_folder'\trend_dd",replace)


*Create coefficient graph:
use "`temp_folder'\trend_dd", clear
keep in 1/14
keep parm estimate stderr z p min95 max95
*Make one parameter to zero, to use as year before introduction:
replace estimate = 0 in 14
replace min95 = 0 in 14
replace max95 = 0 in 14
replace stderr = 0 in 14
*Make the results appear in correct order:
generate date = _n
replace date = 2.5 if date==14
sort date
drop date
generate date = _n
label var date "Term"
*Produce plot:
graph twoway (scatter estimate date) (line estimate date) (rcap min95 max95 date, color(black) lwidth(medium)), xlabel(1 "Spring 2008" 4 "Fall 2009" 7 "Spring 2011" 10 "Fall 2012" 13 "Spring 2014") legend(row(1) order( 1 "Treat-term effects" 3 "95 % C.I." ) ) title("Gender difference std score") yline(0) xline(3)
graph export "`graph_folder'\trend_dd.tif", as(tif) width(1245) height(906) replace



*Controling for multiple choice:
********************************
use "`nek_folder'\quick_nek_long", clear
eststo clear
*Score on question:
eststo: ivreg2 std_score female09 HT09 female_student mltplc_points, cl(teacherid newid)

*Collapse in order get weighting of observations right, since students might answer different no of questions:
collapse (mean) future_econ b_level c_level, by(newid date female_student female09 HT09 term pass_course mltplc_points)
label var b_level "Econ minor"
label var c_level "Econ major"
*Minor:
eststo: ivreg2 b_level female09 HT09 female_student mltplc_points if term<9, cl(newid)
*Major:
eststo: ivreg2 c_level female09 HT09 female_student mltplc_points if term<9, cl(newid)
*Robustness for basic diff-in-diff on student score:
*Full diff-in-diff:
use "`nek_folder'\DD_data", clear
eststo: reg std_score treat essay_09 female_essay essay female09 HT09 female_student, cl(newid)

esttab using "`table_folder'\mltplc_control_DD.rtf", keep(female09 treat) s(N) se label title(Multiple choice predictions) addnote("Note: Standard errors clustered at the  student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace



*Table looking into same sex bias:
**********************************
*Main same sex:
eststo clear
*Read in data:
use "`nek_folder'\quick_nek_long", clear
*Adding date fes for identification:
quietly tab date, gen(date_fes)
*Only pre-period:
*Same sex main spec, clustering on both TA and student:
eststo: ivreg2 std_score same_sex date_fes1-date_fes5 if HT09==0, cl(teacherid newid)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "Yes"
*Same sex main spec, clustering on both TA and student, including anonymous period:
eststo: ivreg2 std_score same_sex same_sex09 date_fes1-date_fes28, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "No"
*Run separate regression by male teacher:
eststo: ivreg2 std_score same_sex same_sex09 date_fes1-date_fes28 if female_teacher==0, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "Yes"
estadd local femaleTAs_only "No"
estadd local onlypre "No"
*Run separate regression by female teacher:
eststo: ivreg2 std_score same_sex same_sex09 date_fes1-date_fes28 if female_teacher==1, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "Yes"
estadd local onlypre "No"
*Expand to look at difference compared to women, ie cross discrimination:
eststo: ivreg2 std_score same_sex same_sex09 date_fes1-date_fes28 female_student female09, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "No"


esttab using "`table_folder'\Samesexresults.rtf", keep(same_sex same_sex09 female_student female09) s(sumafter waldp questionFEs maleTAs_only femaleTAs_only onlypre N, label("Sum treatments" "P-value" "Exam FEs" "Male TAs only" "Female TAs only" "Only pre-period")) se label title(Results same-sex bias) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace




*Testing randomization of teacher to question:
**********************************************
*At TA gender:
*Graphical test:
use "`nek_folder'\quick_nek_long", clear
*Pre-period:
*Female student:
ivreg2 female_student female_teacher if HT09==0, cl(teacherid)
estimates store A1

*Multiple choice score:
ivreg2 mltplc_score female_teacher if HT09==0, cl(teacherid newid)
estimates store B1
*Multiple choice score, men:
ivreg2 mltplc_points_men female_teacher if HT09==0, cl(teacherid newid)
estimates store C1
*Multiple choice score, women:
ivreg2 mltplc_points_women female_teacher if HT09==0, cl(teacherid newid)
estimates store D1

*Age:
ivreg2 age_exam female_teacher if HT09==0, cl(teacherid newid)
estimates store E1
*Age, men
ivreg2 age_men female_teacher if HT09==0, cl(teacherid)
estimates store F1
*Age, women
ivreg2 age_women female_teacher if HT09==0, cl(teacherid newid)
estimates store G1

*Ethnic minority:
ivreg2 ethn_min female_teacher if HT09==0, cl(teacherid)
estimates store H1
*Ethnic minority, men
ivreg2 ethn_min_men female_teacher if HT09==0, cl(teacherid)
estimates store I1
*Ethnic minority, women
ivreg2 ethn_min_women female_teacher if HT09==0, cl(teacherid)
estimates store J1

*Swedish name:
ivreg2 sweden female_teacher if HT09==0, cl(teacherid)
estimates store K1
*Swedish name, men:
ivreg2 sweden_men female_teacher if HT09==0, cl(teacherid)
estimates store L1
*Swedish name, women:
ivreg2 sweden_women female_teacher if HT09==0, cl(teacherid)
estimates store M1

*Retake:
ivreg2 retake female_teacher if HT09==0, cl(teacherid newid)
estimates store N1



*Post:
*Female student:
ivreg2 female_student female_teacher if HT09==1, cl(teacherid newid)
estimates store A2

*Multiple choice score:
ivreg2 mltplc_score female_teacher if HT09==1, cl(teacherid newid)
estimates store B2
*Multiple choice score, men:
ivreg2 mltplc_points_men female_teacher if HT09==1, cl(teacherid newid)
estimates store C2
*Multiple choice score, women:
ivreg2 mltplc_points_women female_teacher if HT09==1, cl(teacherid newid)
estimates store D2

*Age:
ivreg2 age_exam female_teacher if HT09==1, cl(teacherid newid)
estimates store E2
*Age, men
ivreg2 age_men female_teacher if HT09==1, cl(teacherid newid)
estimates store F2
*Age, women
ivreg2 age_women female_teacher if HT09==1, cl(teacherid newid)
estimates store G2

*Ethnic minority:
ivreg2 ethn_min female_teacher if HT09==1, cl(teacherid newid)
estimates store H2
*Ethnic minority, men
ivreg2 ethn_min_men female_teacher if HT09==1, cl(teacherid)
estimates store I2
*Ethnic minority, women
ivreg2 ethn_min_women female_teacher if HT09==1, cl(teacherid newid)
estimates store J2

*Swedish name:
ivreg2 sweden female_teacher if HT09==1, cl(teacherid newid)
estimates store K2
*Swedish name, men:
ivreg2 sweden_men female_teacher if HT09==1, cl(teacherid)
estimates store L2
*Swedish name, women:
ivreg2 sweden_women female_teacher if HT09==1, cl(teacherid newid)
estimates store M2


*Retake:
ivreg2 retake female_teacher if HT09==1, cl(teacherid newid)
estimates store N2

coefplot (A1, aseq(Female student) \ B1, aseq(Multiple-choice points) \ C1, aseq(Multiple-choice points, men) \ D1, aseq(Multiple-choice points, women) \ E1, aseq(Age) \ F1, aseq(Age, men) \ G1,aseq(Age, women) \ H1,aseq(Ethn. minority) \ I1,aseq(Ethn. minority, men) \ J1,aseq(Ethn. minority, women) \ K1,aseq(Swedish name) \ L1,aseq(Swedish name, men) \ M1,aseq(Swedish name, women) \ N1, aseq(Probability of retake)) || (A2, aseq(Female student) \ B2, aseq(Multiple-choice points) \ C2, aseq(Multiple-choice points, men) \ D2, aseq(Multiple-choice points, women) \ E2, aseq(Age) \ F2, aseq(Age, men) \ G2,aseq(Age, women)  \ H2,aseq(Ethn. minority) \ I2,aseq(Ethn. minority, men) \ J2,aseq(Ethn. minority, women) \ K2,aseq(Swedish name) \ L2,aseq(Swedish name, men) \ M2,aseq(Swedish name, women) \ N2, aseq(Probability of retake)), keep(female_teacher) xline(0) swap bylabels("Pre" "Post")
graph export "`graph_folder'\balance_femaleTA.tif", replace width(1245) height(906)


*Balance test on samesex level:
*******************************
*Graphical test:
use "`nek_folder'\quick_nek_long", clear
*Pre-period:
*Multiple choice score:
ivreg2 mltplc_score same_sex if HT09==0, cl(teacherid newid)
estimates store B1
*Multiple choice score, men:
ivreg2 mltplc_points_men same_sex if HT09==0, cl(teacherid newid)
estimates store C1
*Multiple choice score, women:
ivreg2 mltplc_points_women same_sex if HT09==0, cl(teacherid newid)
estimates store D1

*Age:
ivreg2 age_exam same_sex if HT09==0, cl(teacherid newid)
estimates store E1
*Age, men
ivreg2 age_men same_sex if HT09==0, cl(teacherid)
estimates store F1
*Age, women
ivreg2 age_women same_sex if HT09==0, cl(teacherid newid)
estimates store G1

*Ethnic minority:
ivreg2 ethn_min same_sex if HT09==0, cl(teacherid newid)
estimates store H1
*Ethnic minority, men
ivreg2 ethn_min_men same_sex if HT09==0, cl(teacherid)
estimates store I1
*Ethnic minority, women
ivreg2 ethn_min_women same_sex if HT09==0, cl(teacherid)
estimates store J1

*Swedish name:
ivreg2 sweden same_sex if HT09==0, cl(teacherid newid)
estimates store K1
*Swedish name, men:
ivreg2 sweden_men same_sex if HT09==0, cl(teacherid)
estimates store L1
*Swedish name, women:
ivreg2 sweden_women same_sex if HT09==0, cl(teacherid)
estimates store M1

*Retake:
ivreg2 retake same_sex if HT09==0, cl(teacherid newid)
estimates store N1


*Post:
*Multiple choice score:
ivreg2 mltplc_score same_sex if HT09==1, cl(teacherid newid)
estimates store B2
*Multiple choice score, men:
ivreg2 mltplc_points_men same_sex if HT09==1, cl(teacherid newid)
estimates store C2
*Multiple choice score, women:
ivreg2 mltplc_points_women same_sex if HT09==1, cl(teacherid newid)
estimates store D2

*Age:
ivreg2 age_exam same_sex if HT09==1, cl(teacherid newid)
estimates store E2
*Age, men
ivreg2 age_men same_sex if HT09==1, cl(teacherid newid)
estimates store F2
*Age, women
ivreg2 age_women same_sex if HT09==1, cl(teacherid newid)
estimates store G2

*Ethnic minority:
ivreg2 ethn_min same_sex if HT09==1, cl(teacherid newid)
estimates store H2
*Ethnic minority, men
ivreg2 ethn_min_men same_sex if HT09==1, cl(teacherid)
estimates store I2
*Ethnic minority, women
ivreg2 ethn_min_women same_sex if HT09==1, cl(teacherid newid)
estimates store J2

*Swedish name:
ivreg2 sweden same_sex if HT09==1, cl(teacherid newid)
estimates store K2
*Swedish name, men:
ivreg2 sweden_men same_sex if HT09==1, cl(teacherid)
estimates store L2
*Swedish name, women:
ivreg2 sweden_women same_sex if HT09==1, cl(teacherid newid)
estimates store M2

*Retake:
ivreg2 retake same_sex if HT09==1, cl(teacherid newid)
estimates store N2


coefplot (B1, aseq(Multiple-choice points) \ C1, aseq(Multiple-choice points, men) \ D1, aseq(Multiple-choice points, women) \ E1, aseq(Age) \ F1, aseq(Age, men) \ G1,aseq(Age, women) \ H1,aseq(Ethn. minority) \ I1,aseq(Ethn. minority, men) \ J1,aseq(Ethn. minority, women) \ K1,aseq(Swedish name) \ L1,aseq(Swedish name, men) \ M1,aseq(Swedish name, women) \ N1, aseq(Probability of retake)) || (B2, aseq(Multiple-choice points) \ C2, aseq(Multiple-choice points, men) \ D2, aseq(Multiple-choice points, women) \ E2, aseq(Age) \ F2, aseq(Age, men) \ G2,aseq(Age, women)  \ H2,aseq(Ethn. minority) \ I2,aseq(Ethn. minority, men) \ J2,aseq(Ethn. minority, women) \ K2,aseq(Swedish name) \ L2,aseq(Swedish name, men) \ M2,aseq(Swedish name, women) \ N2, aseq(Probability of retake)), keep(same_sex) xline(0) swap bylabels("Pre" "Post")
graph export "`graph_folder'\balance_samesex.tif", replace width(1245) height(906)




*Summary statistics:
********************
*Nek:
*Read in data:
use "`nek_folder'\quick_nek_long", clear

*Table of summary statistics:
*****************************
eststo clear
label var same_sex "same sex"
estpost sum female_student female_teacher same_sex HT09 retake age_exam age_men age_women ethn_min ethn_min_men ethn_min_women sweden sweden_men sweden_women std_score mltplc_points poang10 poang12 if same_sex!=.
esttab using "`table_folder'\summarystats_nek.rtf", cells("mean sd min max") nomtitle nonumber label replace

eststo clear
estpost sum female_student female_teacher same_sex HT09 retake age_exam age_men age_women ethn_min ethn_min_men ethn_min_women sweden sweden_men sweden_women std_score mltplc_points poang10 poang12 if same_sex!=. & female_teacher==0
esttab using "`table_folder'\summarystats_nek_maleTA.rtf", cells("mean sd min max") nomtitle nonumber label replace

eststo clear
estpost sum female_student female_teacher same_sex HT09 retake age_exam age_men age_women ethn_min ethn_min_men ethn_min_women sweden sweden_men sweden_women std_score mltplc_points poang10 poang12 if same_sex!=. & female_teacher==1
esttab using "`table_folder'\summarystats_nek_femaleTA.rtf", cells("mean sd min max") nomtitle nonumber label replace





*Appendix:
**********
*Table looking into same sex bias without question fixed effects:
*****************************************************************
use "`nek_folder'\quick_nek_long", clear
eststo clear
*Only pre-period:
*Same sex main spec, clustering on both TA and student:
eststo: ivreg2 std_score same_sex if HT09==0, cl(teacherid newid)
estadd local questionFEs "No"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "Yes"
*Same sex main spec, clustering on both TA and student, including anonymous period:
eststo: ivreg2 std_score same_sex same_sex09 HT09, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "No"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "No"
*Run separate regression by male teacher:
eststo: ivreg2 std_score same_sex same_sex09 HT09 if female_teacher==0, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "No"
estadd local maleTAs_only "Yes"
estadd local femaleTAs_only "No"
estadd local onlypre "No"
*Run separate regression by female teacher:
eststo: ivreg2 std_score same_sex same_sex09 HT09 if female_teacher==1, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "No"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "Yes"
estadd local onlypre "No"
*Expand to look at difference compared to women, ie cross discrimination:
eststo: ivreg2 std_score same_sex same_sex09 female_student female09 HT09, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "No"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "No"

esttab using "`table_folder'\Samesexresults_noQFEs.rtf", keep(same_sex same_sex09 female_student female09) s(sumafter waldp questionFEs maleTAs_only femaleTAs_only onlypre N, label("Sum treatments" "P-value" "Question FEs" "Male TAs only" "Female TAs only" "Only pre-period")) se label title(Results same-sex bias, no question fixed effects) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace



*Table looking into same sex bias with question fixed effects:
**************************************************************
eststo clear
*Read in data:
use "`nek_folder'\quick_nek_long", clear
*Adding question fes:
egen question =  group(date questionno)
quietly tab question, gen(question_fes)
*Only pre-period:
*Same sex main spec, clustering on both TA and student:
eststo: ivreg2 std_score same_sex question_fes1-question_fes34 if HT09==0, cl(teacherid newid)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "Yes"
*Same sex main spec, clustering on both TA and student, including anonymous period:
eststo: ivreg2 std_score same_sex same_sex09 question_fes1-question_fes194, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "No"
*Run separate regression by male teacher:
eststo: ivreg2 std_score same_sex same_sex09 question_fes1-question_fes194 if female_teacher==0, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "Yes"
estadd local femaleTAs_only "No"
estadd local onlypre "No"
*Run separate regression by female teacher:
eststo: ivreg2 std_score same_sex same_sex09 question_fes1-question_fes194 if female_teacher==1, cl(teacherid newid)
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "Yes"
estadd local onlypre "No"
*Expand to look at difference compared to women, ie cross discrimination:
eststo: ivreg2 std_score same_sex same_sex09 question_fes1-question_fes194 female_student female09, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local maleTAs_only "No"
estadd local femaleTAs_only "No"
estadd local onlypre "No"

esttab using "`table_folder'\Samesexresults_QFEs.rtf", keep(same_sex same_sex09 female_student female09) s(sumafter waldp questionFEs maleTAs_only femaleTAs_only onlypre N, label("Sum treatments" "P-value" "Question FEs" "Male TAs only" "Female TAs only" "Only pre-period")) se label title(Results same-sex bias) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace





*Adding different controls for appendix:
****************************************
use "`nek_folder'\quick_nek_long", clear
egen question =  group(date questionno)
quietly tab question, gen(question_fes)

egen program_fem =  group(program_stud female_student)
quietly tab program_fem, gen(program_fes)

eststo clear
*Question FEs:
eststo: ivreg2 std_score female09 female_student HT09 question_fes1-question_fes194, cl(teacherid newid)
estadd local questionFEs "Yes"
estadd local controls "No"
estadd local programFEs "No"
estadd local lineartrend "No"
*Additional controls:
eststo: ivreg2 std_score female09 female_student HT09 question_fes1-question_fes194 age_exam ethn_min sweden, cl(teacherid newid)
estadd local questionFEs "Yes"
estadd local controls "Yes"
estadd local programFEs "No"
estadd local lineartrend "No"
*Program FEs:
eststo: ivreg2 std_score female09 female_student HT09 question_fes1-question_fes194 age_exam ethn_min sweden program_fes1-program_fes73, cl(teacherid newid)
estadd local questionFEs "Yes"
estadd local controls "Yes"
estadd local programFEs "Yes"
estadd local lineartrend "No"
*Linear trend:
gen term_female=term*female_student
eststo:  ivreg2 std_score female09 HT09 term term_female female_student, cl(teacherid newid)
estadd local questionFEs "No"
estadd local controls "No"
estadd local programFEs "No"
estadd local lineartrend "Yes"
*Question FEs:
eststo: ivreg2 std_score same_sex same_sex09 female_student female09 question_fes1-question_fes194, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local controls "No"
estadd local programFEs "No"
estadd local lineartrend "No"
*Additional controls:
eststo: ivreg2 std_score same_sex same_sex09 female_student female09 question_fes1-question_fes194 age_exam ethn_min sweden, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local controls "Yes"
estadd local programFEs "No"
estadd local lineartrend "No"
*Program FEs:
eststo: ivreg2 std_score female09 female_student same_sex same_sex09 HT09 question_fes1-question_fes194 age_exam ethn_min sweden program_fes1-program_fes73, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "Yes"
estadd local controls "Yes"
estadd local programFEs "Yes"
estadd local lineartrend "No"
*Linear trend:
eststo:  ivreg2 std_score same_sex same_sex09 female09 HT09 term term_female female_student, cl(teacherid newid)
*Adding sum of effects:
estadd scalar sumafter=_b[same_sex]+_b[same_sex09]
*Wald test on b1+b2=0:
test same_sex+same_sex09 = 0
estadd scalar waldp=r(p)
estadd local questionFEs "No"
estadd local controls "No"
estadd local programFEs "No"
estadd local lineartrend "Yes"

esttab using "`table_folder'\diff_diff_controls.rtf", keep(female09 female_student same_sex09 same_sex) s(sumafter waldp questionFEs controls programFEs lineartrend N, label("Sum treatments" "P-value" "Question FEs" "Additional controls"  "Gender*program FEs" "Linear trend")) se label title(Table 1: Adding controls) addnote("Note: Standard errors clustered at the TA and student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace





*Balancing table:
*****************
use "`nek_folder'\quick_nek_long", clear
eststo clear
*Compare means using regressions in order to get standard errors right
*Pre
*Share women:
eststo clear
eststo: ivreg2 female_student if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 female_student if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
*Does not work, why? Cluster at student only?
eststo: ivreg2 female_student female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_female_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA level when computing p-values.") nostar b(3) replace

*Multiplc score:
eststo clear
ivreg2 mltplc_points if female_teacher==1 & HT09==0, cl(teacherid newid)
eststo: ivreg2 mltplc_points if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 mltplc_points if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 mltplc_points female_teacher if HT09==0, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_mltplc_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Multiplec score men:
eststo clear
eststo: ivreg2 mltplc_points_men if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 mltplc_points_men if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 mltplc_points_men female_teacher if HT09==0, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_mltplc_men_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Multiplec score women: 
eststo clear
eststo: ivreg2 mltplc_points_women if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 mltplc_points_women if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 mltplc_points_women female_teacher if HT09==0, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_mltplc_women_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Age:
eststo clear
eststo: ivreg2 age_exam if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 age_exam if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 age_exam female_teacher if HT09==0, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_age_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Age, men:
eststo clear
eststo: ivreg2 age_men if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 age_men if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 age_men female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_age_men_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Age, women:
eststo clear
eststo: ivreg2 age_women if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 age_women if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 age_women female_teacher if HT09==0, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_age_women_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Ethnic minority:
eststo clear
eststo: ivreg2 ethn_min if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 ethn_min if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 ethn_min female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_ethnicity_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace


*Ethnic minority, men:
eststo clear
eststo: ivreg2 ethn_min_men if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 ethn_min_men if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 ethn_min_men female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_ethnicity_men_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace


*Ethnic minority, women:
eststo clear
eststo: ivreg2 ethn_min_women if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 ethn_min_women if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 ethn_min_women female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_ethnicity_women_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Swedish name:
eststo clear
eststo: ivreg2 sweden if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 sweden if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 sweden female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_sweden_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Swedish name, men:
eststo clear
eststo: ivreg2 sweden_men if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 sweden_men if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 sweden_men female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_sweden_men_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Swedish name, women:
eststo clear
eststo: ivreg2 sweden_women if female_teacher==1 & HT09==0, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 sweden_women if female_teacher==0 & HT09==0, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 sweden_women female_teacher if HT09==0, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_sweden_women_pre.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace


*Post:
*Share women:
eststo clear
eststo: ivreg2 female_student if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 female_student if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
*Does not work, why? Cluster at student only?
eststo: ivreg2 female_student female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_female_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Multiplc score:
eststo clear
eststo: ivreg2 mltplc_points if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 mltplc_points if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 mltplc_points female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_mltplc_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Multiplec score men:
eststo clear
eststo: ivreg2 mltplc_points_men if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 mltplc_points_men if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 mltplc_points_men female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_mltplc_men_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Multiplec score women: 
eststo clear
eststo: ivreg2 mltplc_points_women if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 mltplc_points_women if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 mltplc_points_women female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_mltplc_women_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Age:
eststo clear
eststo: ivreg2 age_exam if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 age_exam if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 age_exam female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_age_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Age, men:
eststo clear
eststo: ivreg2 age_men if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 age_men if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 age_men female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_age_men_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Age, women:
eststo clear
eststo: ivreg2 age_women if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 age_women if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 age_women female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_age_women_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Ethnic minority:
eststo clear
eststo: ivreg2 ethn_min if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 ethn_min if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 ethn_min female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_ethnicity_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace


*Ethnic minority, men:
eststo clear
eststo: ivreg2 ethn_min_men if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 ethn_min_men if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 ethn_min_men female_teacher if HT09==1, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_ethnicity_men_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace


*Ethnic minority, women:
eststo clear
eststo: ivreg2 ethn_min_women if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 ethn_min_women if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 ethn_min_women female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_ethnicity_women_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace


*Swedish name:
eststo clear
eststo: ivreg2 sweden if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 sweden if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 sweden female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_sweden_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Swedish name, men:
eststo clear
eststo: ivreg2 sweden_men if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 sweden_men if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 sweden_men female_teacher if HT09==1, cl(teacherid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_sweden_men_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace

*Swedish name, women:
eststo clear
eststo: ivreg2 sweden_women if female_teacher==1 & HT09==1, cl(teacherid newid)
estadd local TAs "female TAs"
eststo: ivreg2 sweden_women if female_teacher==0 & HT09==1, cl(teacherid newid)
estadd local TAs "male TAs"
eststo: ivreg2 sweden_women female_teacher if HT09==1, cl(teacherid newid)
estadd local TAs "diff."

esttab using "`table_folder'\Balancing_sweden_women_post.rtf", s(N TAs) p label title(Table 1: Balance test) addnote("Note: Standard errors clustered at the TA (49 clusters) and student (6 521 clusters) level when computing p-values.") nostar b(3) replace



*Figure over observed share of business students:
*************************************************
use "`nek_folder'\quick_nek_long", clear
*Ekonomprogrammet:
gen FEK_stud=1 if program_stud=="SFEKK"
*Management och marknadsföring:
replace FEK_stud=1 if program_stud=="SFMAK"
*Finans och redovisning:
replace FEK_stud=1 if program_stud=="SFREK"
*Civilek:
replace FEK_stud=1 if program_stud=="SCEKY"
replace FEK_stud=1 if program_stud=="SCIVY"
*Ekonomlinjen:
replace FEK_stud=1 if program_stud=="SEKOM"
*Detaljhandeln:
replace FEK_stud=1 if program_stud=="SFIDK"
*Redovisning och finans:
replace FEK_stud=1 if program_stud=="SFIRK"
*Marknadsföring och management:
replace FEK_stud=1 if program_stud=="SFMMK"
replace FEK_stud=0 if FEK_stud==.


collapse (mean) FEK_stud, by(term)
drop if FEK_stud==.

label var FEK_stud "Share business students"
label var term "Term"
tsset term
graph twoway tsline FEK_stud , recast(connected) ylabel(1(0.2)0) xlabel(1 "Spring 2008" 4 "Fall 2009" 7 "Spring 2011" 10 "Fall 2012" 13 "Spring 2014")
graph export "`graph_folder'\share_business.tif", as(tif) width(1245) height(906) replace




*"Add back" missing business students:
**************************************
*Add FEK students from next year to syear6:
*Generate indicator for FEK student (exclude old programs that are doing retakes):
use "`nek_folder'\quick_nek_long", clear
*Ekonomprogrammet:
gen FEK_stud=1 if program_stud=="SFEKK"
*Management och marknadsföring:
replace FEK_stud=1 if program_stud=="SFMAK"
*Finans och redovisning:
replace FEK_stud=1 if program_stud=="SFREK"
*Civilek:
replace FEK_stud=1 if program_stud=="SCEKY"
replace FEK_stud=1 if program_stud=="SCIVY"
*Ekonomlinjen:
replace FEK_stud=1 if program_stud=="SEKOM"
*Detaljhandeln:
replace FEK_stud=1 if program_stud=="SFIDK"
*Redovisning och finans:
replace FEK_stud=1 if program_stud=="SFIRK"
*Marknadsföring och management:
replace FEK_stud=1 if program_stud=="SFMMK"
replace FEK_stud=0 if FEK_stud==.

*Keep those in the following year:
keep if FEK_stud==1 & syear==7
replace syear=6
replace term=10 if term==12
replace term=11 if term==13


append using "`nek_folder'\quick_nek_long"

quietly tab  term, gen(term_fe)
*Generate necessary interactions:
forvalues l=1/14 {
	*Female*date:
	gen term_female`l'=female_student*term_fe`l'
}

*Adding question fes:
egen question =  group(date questionno)
quietly tab question, gen(question_fes)


ivreg2 std_score term_female1-term_female2 term_female4-term_female14 question_fes1-question_fes194 female_student age_exam sweden ethn_min, cl(teacherid newid)
parmest, saving("`temp_folder'\trend_dd",replace)


*Create coefficient graph:
use "`temp_folder'\trend_dd", clear
keep in 1/14
keep parm estimate stderr z p min95 max95
*Make one parameter to zero, to use as year before introduction:
replace estimate = 0 in 14
replace min95 = 0 in 14
replace max95 = 0 in 14
replace stderr = 0 in 14
*Make the results appear in correct order:
generate date = _n
replace date = 2.5 if date==14
sort date
drop date
generate date = _n
label var date "Term"
*Produce plot:
graph twoway (scatter estimate date) (line estimate date) (rcap min95 max95 date, color(black) lwidth(medium)), xlabel(1 "Spring 2008" 4 "Fall 2009" 7 "Spring 2011" 10 "Fall 2012" 13 "Spring 2014") legend(row(1) order( 1 "Treat-term effects" 3 "95 % C.I." ) ) title("Gender difference std score") yline(0) xline(3)
graph export "`graph_folder'\trend_dd_FEKstudents.tif", as(tif) width(1245) height(906) replace






*Relationship between multiple choice and outcomes:
use "`nek_folder'\quick_nek_long", clear
eststo clear

*Relationship between multiple choice score and essay score:
eststo: reg std_score mltplc_points, cl(newid)
*Collapse in order get weighting of observations right, since students might answer different no of questions:
collapse (mean) future_econ b_level c_level, by(newid date female_student female09 HT09 term pass_course mltplc_points)
label var b_level "Econ minor"
label var c_level "Econ major"
*Relationship between multiple choice score and minor:
eststo: reg b_level mltplc_points if term<9, cl(newid)
*Relationship between multiple choice score and major:
eststo: reg c_level mltplc_points if term<9, cl(newid)

esttab using "`table_folder'\mltplc_prediction.rtf", keep(mltplc_points) s(N) se label title(Multiple choice predictions) addnote("Note: Standard errors clustered at the  student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace





*Results based on grades for micro, macro, business and political science:
*Look at grades for these departments intro courses:
use "`nek_folder'\intro_grades", clear
eststo clear
*Macro based on grades:
eststo: reg std_score female09 female_student HT09 if macro==1, cl(newid)
estadd local course "Intro macro"
estadd local years "2008-2014"
*Micro:
eststo: reg std_score female09 female_student HT09 if micro==1, cl(newid)
estadd local course "Intro micro"
estadd local years "2008-2014"
*Joint:
eststo: reg std_score female09 female_student HT09 if micro==1 | macro==1, cl(newid)
estadd local course "Intro micro & macro"
estadd local years "2008-2014"
*Business:
eststo: reg std_score female09 female_student HT09 if fek==1 | pol_science==1, cl(newid)
estadd local course "Intro business & intro political science"
estadd local years "2008-2014"

esttab using "`table_folder'\grades_diff_diff.rtf", keep(female09) s(course years N, label("Course" "Years")) se label title(Results across departments) addnote("Note: Standard errors clustered at the TA and student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace




*Future degrees:
use "`nek_folder'\quick_nek_long", clear
eststo clear

*Collapse in order get weighting of observations right, since students might answer different no of questions:
collapse (mean) no_major other_major polfek_major c_level, by(newid date female_student female09 HT09 term pass_course)

label var no_major "No Major"
label var other_major "Other Major"
label var polfek_major "Major Business or Political science"
label var c_level "Major Economics"

*Econ (replication)
eststo: ivreg2 c_level female09 female_student HT09 if term<9, cl(newid)
*FEK:
eststo: ivreg2 polfek_major female09 female_student HT09 if term<9, cl(newid)
*Pol science:
eststo: ivreg2 other_major female09 female_student HT09 if term<9, cl(newid)
*Any major:
eststo: ivreg2 no_major female09 female_student HT09 if term<9, cl(newid)

esttab using "`table_folder'\degrees_other.rtf", keep(female09) s(N) se label title(Majoring in other subjects) addnote("Note: Standard errors clustered at the TA and student level.") star(* 0.10 ** 0.05 *** 0.01) b(3) replace










*Share political science or business students taking the intro course in economics:
use "`nek_folder'\intro_grades", clear
replace micro=0 if micro==.
replace macro=0 if macro==.
replace fek=0 if fek==.
replace pol_science=0 if pol_science==.
*Collapse into individual students:
collapse (mean) micro macro fek pol_science, by(newid)
*Keep only those that took the micro or macro course:
drop if micro==0 | macro==0
*Generate dummy for taking intro business or political science:
gen intro_fek_pol=1 if fek>0 | pol_science>0
replace intro_fek_pol=0 if intro_fek_pol==.
tab intro_fek_pol
*Around 60 % take business or political science intro course also!



use "`nek_folder'\quick_nek_long", clear
drop if no_major==1
collapse (mean) polfek_major, by(newid)
tab polfek_major
*76 %




